//
//  UpgradeView.m
//  Api
//
//  Created by CPX on 2018/8/20.
//

#import "UpgradeView.h"
#import "Global.h"
#import "YYCategories.h"
#import "UIImage+RTL.h"
#import "NSString+Helper.h"
//#import "LanguageManager.h"

/**
 更新View
 @author yzl
 */
@interface UpgradeView()

@property (nonatomic,strong) UIScrollView *sc;
@property (nonatomic,strong) UIButton *closeBtn;
@property (nonatomic,strong) UILabel *yv;
//@property (nonatomic,strong) UILa

@end

@implementation UpgradeView

- (void)initUI{
    [super initUI];

    self.removeOnTouchOutside = NO;
    UIView *view = [UIView new];
    [self.contentView addSubview:view];
    CGFloat w = 300;
    if (kScreenWidth == 320) {
        w = 320 - 60;
    }
    view.frame = CGRectMake(0, 0, w, 460);
    view.backgroundColor = [UIColor whiteColor];
    view.centerX = self.contentView.centerX;
    view.centerY = self.contentView.height/2;
    view.layer.cornerRadius = 5;
    view.layer.masksToBounds = YES;
    
    
    
    
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage RTL_ImageWithName:@"update_img"]];
    img.frame = CGRectMake(0, 0, w, w/5*3);
    [view addSubview:img];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageNamed:@"upclose_btn_black"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(onCloseTouched) forControlEvents:UIControlEventTouchUpInside];
    closeBtn.frame = CGRectMake(0, 0, 31, 31);
    closeBtn.right = view.width;
    [view addSubview:closeBtn];
    self.closeBtn = closeBtn;
    
    UIView *nvview = [UIView new];
    nvview.frame = CGRectMake(0, 0, 44, 22);
    nvview.backgroundColor = UIColorHex(#F76B1C);
    nvview.right = view.width - 31;
    nvview.centerY = img.bottom-10;
    nvview.layer.masksToBounds = YES;
    nvview.layer.cornerRadius = 11;
    [view addSubview:nvview];
    
    self.nvLabel = [UILabel new];
    self.nvLabel.font = [UIFont systemFontOfSize:14];
    self.nvLabel.text = @"3.0";
    self.nvLabel.textColor = [UIColor whiteColor];
    self.nvLabel.frame = nvview.bounds;
    self.nvLabel.textAlignment = NSTextAlignmentCenter;
    [nvview addSubview:self.nvLabel];
    
    UILabel *label = [UILabel new];
    label.font = [UIFont boldSystemFontOfSize:20];
    label.textColor = UIColorHex(#000A05);
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [self getStr:@"new_version"];
    [label sizeToFit];
    label.top = img.bottom + 6;
    label.centerX = img.centerX;
    [view addSubview:label];
    
    UILabel *label2 = [UILabel new];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    label2.text = [NSString stringWithFormat:@"%@ %@",[self getStr:@"your_version"],app_Version];
    label2.textColor = UIColorHex(#929292);
    label2.font = [UIFont systemFontOfSize:14];
    label2.textAlignment = NSTextAlignmentCenter;
    label2.top = label.bottom + 5;
    [label2 sizeToFit];
    label2.top = label.bottom+5;
    label2.centerX = label.centerX;
    [view addSubview:label2];
    self.yv = label2;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, w-60, (w-60)/238*44);
    btn.bottom = view.height - 12.5;
    btn.backgroundColor = UIColorHex(#2B9958);
    btn.layer.cornerRadius = btn.height/2;
    btn.layer.masksToBounds = YES;
    btn.centerX = view.width/2;
    [btn addTarget:self action:@selector(goToAppStore) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:[self getStr:@"update_now"] forState:UIControlStateNormal];
    
    self.sc = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, w-60, btn.top - label2.bottom - 18.5-23.5)];
//    self.sc.backgroundColor = [UIColor yellowColor];
    self.sc.top = label2.bottom + 18.5;
    self.sc.centerX = btn.centerX;
    
    [view addSubview:btn];
    [view addSubview:self.sc];
    
    self.infoLabel = [UILabel new];
    self.infoLabel.text = @"";
    self.infoLabel.font = [UIFont systemFontOfSize:14];
    self.infoLabel.textColor = UIColorHex(#929292);
    self.infoLabel.numberOfLines = 0;
    [self.sc addSubview:self.infoLabel];
    
    if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
        closeBtn.left = 0;
        nvview.left = 31;
    }
}

- (void)setData:(UpgradeModel *)data{
    _data = data;
//    self.closeBtn.hidden = !data.force_update;
    
    self.nvLabel.text = data.version;
    
//    self.infoLabel.
    
    NSMutableAttributedString *ptattrtx = [[NSMutableAttributedString alloc] initWithData:[data.changes?data.changes:@"" dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
    
    CGSize size = [ptattrtx boundingRectWithSize:CGSizeMake(self.sc.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
    self.infoLabel.attributedText = ptattrtx;
    self.infoLabel.size = size;
    self.sc.contentSize = size;
    
    if ([[self getLan] isEqualToString:@"ar"]) {
        self.nvLabel.text = self.nvLabel.text.arabicString;
        self.yv.text = self.yv.text.arabicString;
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
}

- (void)onCloseTouched{
    if (self.data.force_update) {
        [self goToAppStore];
    }
    else{
        [self removeFromSuperview];
    }
    
}

- (NSString*)getStr:(NSString*)name{
    Class c = NSClassFromString(@"LanguageManager");
    NSString *str = @"";
    if (c) {
        SEL sel1 = NSSelectorFromString(@"defaultManager");
        id ins = [c performSelector:sel1];
        SEL sel2 = NSSelectorFromString(@"getStringForKey:");
        str = [ins performSelector:sel2 withObject:name];
    }else{
        str = name;
    }
    return str;
}

- (NSString*)getLan{
    
    Class c = NSClassFromString(@"LanguageManager");
    NSString *str = @"";
    if (c) {
        SEL sel1 = NSSelectorFromString(@"defaultManager");
        id ins = [c performSelector:sel1];
        SEL sel2 = NSSelectorFromString(@"appLanguage");
        str = [ins performSelector:sel2];
    }else{
        
    }
    return str;
}

- (void)goToAppStore{
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.data.download_url]];
}

@end
